﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.DB;
using MaxLeaf.Common;
using System.Data;

namespace MaxLeaf.DAL.K2K
{
    /// <summary>
    /// 类别
    /// </summary>
    public class t_SubMessageDAL : BaseDAL
    {
        public t_SubMessageDAL()
            : base()
        {
            strTableName = "t_SubMessage";
            strMapKeyFieldName = "FInterID";
            strDestRealKeyFieldName = "FName";
            strFieldNames = "FBrNo,FID,FParentID,FName,FDetail,FDeleted,FSystemType,FSpec,UUID";
        }
        public override int Update(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strKeyValue)
        {
            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "TableName", strTableName }, { "OrgKeyValue", strKeyValue } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFid = dbDestK3Con.GetValue(strSql, kvp.Value);
            if (strFid.Length > 0)
            {

            }
            else
            {
                DataRow drOrgData = dbOrgK3Con.GetTableDataFirstRow(strTableName, new Dictionary<string, object>() { { strMapKeyFieldName, strKeyValue } });
                //dictOtherKeyFieldsCond = new Dictionary<string, object>() { 
                //    {"FTypeID",drOrgData["FTypeID"]}
                //};
                //dictDestOtherKeyFieldsCond = new Dictionary<string, object>() { 
                //     {"FTypeID",GetFTypeID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FTypeID"]))}
                //};
            }
            return base.Update(dbOrgK3Con, dbDestK3Con, strKeyValue);

            
        }

        protected override DataTable DAL_GetData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, int intdataType, object objParam)
        {
            GetDataType dataType = (GetDataType)intdataType;

            if (dataType == GetDataType.OrgData)
            {
                return Gett_SubMessage(dbOrgK3Con, CvtFunc.ToString(objParam));
            }

            return base.DAL_GetData(dbOrgK3Con, dbDestK3Con, intdataType, objParam);
        }

        protected override void DAL_GetFieldData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FTypeID", StringComparison.InvariantCultureIgnoreCase))
            {
                //subtype key
                drDestData[strFieldName] = GetFTypeID(dbOrgK3Con, dbDestK3Con, CvtFunc.ToString(drOrgData["FTypeID"]));

                 
            }
            else if (strFieldName.Equals("FInterID", StringComparison.InvariantCultureIgnoreCase))
            {
                
                _strFInterID = dbDestK3Con.GetValue("select Max(Finterid)+1 from t_submessage");
                drDestData[strFieldName] = _strFInterID;
               
            }
            else
            {
                base.DAL_GetFieldData(dbOrgK3Con, dbDestK3Con, strFieldName, drOrgData, drDestData);
            }
        }
        protected override void UpdateOtherData(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, DataRow drOrgData, DataRow drData)
        {
             
        }

        private string GetFTypeID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgTypeID)
        {
            DataRow drOrg = dbOrgK3Con.GetTableDataFirstRow("t_SubMesType", new Dictionary<string, object> { { "FTypeID", strOrgTypeID } });
            Updatet_SubMesType(dbDestK3Con, drOrg);

            return strOrgTypeID;
        }

        private void Updatet_SubMesType(DB.DBAccess dbDestK3Con, DataRow drOrg)
        {
            string strKeyFieldName = "FTypeID";
            string strTableName = "t_SubMesType";
            string strFieldNames = "FName,FDetail,FType,FGRType,UUID";

            string strKeyValue = CvtFunc.ToString(drOrg["FTypeID"]);
            DataTable dtData = dbDestK3Con.GetTableData(strTableName, new Dictionary<string, object> { { strKeyFieldName, strKeyValue } }, strKeyFieldName + "," + strFieldNames);
            DataRow drData = null;
            LogStatus logStatus = LogStatus.Update;
            if (dtData.Rows.Count == 0)
            {
                logStatus = LogStatus.Add;
                drData = dtData.NewRow();
                drData[strKeyFieldName] = strKeyValue;

                strFieldNames = strKeyFieldName + "," + strFieldNames;
            }
            else
            {
                drData = dtData.Rows[0];
            }

            string[] arrFieldName = strFieldNames.Split(',');
            foreach (string strField in arrFieldName)
            {
                string strOrgField = strField;


                drData[strField] = drOrg[strOrgField];
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateParamSql(strFieldNames, strTableName, drData, logStatus, strKeyFieldName);

            dbDestK3Con.ExecuteSql(kvp.Key, kvp.Value);
        }

        
        private DataTable Gett_SubMessage(DBAccess dbOrgK3Con, string strFItemID)
        {
            DataTable dtt_SubMessage = null;
            if (dtt_SubMessage == null)
            {
                dtt_SubMessage = dbOrgK3Con.GetTableData("t_SubMessage", new Dictionary<string, object> { { "FInterID", strFItemID } });

            }
            return dtt_SubMessage;
        }
        const string NEWFINTERID="-10000";
        string _strFInterID = null;
        internal string GetFInterID(DBAccess dbOrgK3Con, DBAccess dbDestK3Con, string strOrgFInterID)
        {
            if (strOrgFInterID.Equals("0")  ||strOrgFInterID.Length==0)
            {
                return "0";
            }

            return GetDestMapKeyValue(dbOrgK3Con, dbDestK3Con, strOrgFInterID);
        }
    }

}